package com.example.firstproject.controllor;

import com.example.firstproject.entity.Student;
import com.example.firstproject.mapper.StudentMapper;
import com.example.firstproject.service.StudentService;
import com.example.firstproject.vo.StudentVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@RequestMapping(value="/student")
@Controller
public class StudentControllor {
//    @Autowired
//    private StudentMapper studentMapper;
//    @RequestMapping(value="/register",method = RequestMethod.POST)
//    public String insertStudent(@RequestBody Student student){
//        Student existingStudent = (Student) studentMapper.getStudentById(student.getUsername());
//        if (existingStudent !=null) {
//            return "拆入失败，用户名已存在";
//        }else {
//            studentService.insert(student);
//            return "用户插入成功";
//        }
//    }





    @Autowired
    private StudentService studentService;
    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Map<String, Object> studentRegister(@RequestBody StudentVO studentVO){
        Map<String ,Object> retData = new HashMap<>();
        if(StringUtils.isEmpty(studentVO.getUsername())){
            retData.put("code",500);
            retData.put("message","参数错误");
            return retData;
        }else {
            Student student = new Student(studentVO);
            return studentService.studentReqist(student);
        }
    }
}
